﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Test.Common;
using Test.IDAL;
using Test.Model;

namespace Test.DAL
{
    public class baseDAL<T>:IBaseDAL<T> where T:BaseEntity 
    {
        //private static Model1 _model = null;//每次model都不同，建一个相同的model，两种方法1.
         public static Model1 Model
                {
                    get
                    {
                        //if (_model==null) {
                        //    _model = new Model1();
                        //}
                        //return _model;
                        return new Model1();
                    }
                }
        /// <summary>
        /// 查询集合
        /// </summary>
        /// <param name="expression">表达式</param>
        /// <returns>List集合</returns>
        public  OperResult<List<T>> Select(Expression<Func<T,bool>> expression)
        {
            try
            {

            return OperResult < List < T >>.Success(Model.Set<T>().Where(expression).ToList() );
            }
            catch (Exception ex)
            {
                return OperResult<List<T>>.Failed(ex.Message);
                
            }

        }
        /// <summary>
        /// 查询单个对象
        /// </summary>
        /// <param name="expression">表达式</param>
        /// <returns>单个对象或null</returns>
        public OperResult<T>  Single(Expression<Func<T, bool>> expression)
        {
           
            try
            {

                return OperResult<T>.Success(Model.Set<T>().Find(expression));
            }
            catch (Exception ex)
            {
                return OperResult<T>.Failed(ex.Message);

            }
        }
        /// <summary>
        /// 查询单个对象
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>单个对象或null</returns>
        public OperResult<T>  Single(int id)
        {
           
            try
            {

                return OperResult<T>.Success(Model.Set<T>().Find(id));
            }
            catch (Exception ex)
            {
                return OperResult<T>.Failed(ex.Message);

            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="info">数据</param>
        /// <returns>受影响的行数</returns>
        public OperResult<int>  Insert(T info)
        {
         
            try
            {   
            var model = Model;
            model.Set<T>().Add(info);
            return OperResult<int>.Success(model.SaveChanges());

               
            }
            catch (Exception ex)
            {
                return OperResult<int>.Failed(ex.Message);

            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="info">数据</param>
        /// <returns>受影响的行数</returns>
        public OperResult<int>  Delete(T info)
        {
            
            try
            {/*2.第二种方法*/
            var model = Model;
            var temp = model.Set<T>().FirstOrDefault(p=>p.Id==info.Id);//先继承BaseEntity
            model.Set<T>().Remove(temp);
            return OperResult<int>.Success(model.SaveChanges());
               
            }
            catch (Exception ex)
            {
                return OperResult<int>.Failed(ex.Message);

            }
            
            //Model.Entry<T>(info).State = System.Data.Entity.EntityState.Deleted;
            //return Model.SaveChanges();
        }


    }
}
